/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin			*/
/*	Project:	GlbCloud			*/
/*	Author:		gong_libin			*/
/*	Date:		2012_01_10			*/
/*	File:		MrRwLock.cpp			*/
/********************************************************/

#include "MrRwLock.h"

CMrRwLock::CMrRwLock()
{
}

CMrRwLock::~CMrRwLock()
{
}

int CMrRwLock::MrLockMalloc(void* pLock)
{
	if (NULL == pLock) {
		CMrLock::m_pLock = (pthread_rwlock_t*)malloc(sizeof(pthread_rwlock_t));
		if (NULL == CMrLock::m_pLock) {
			return MR_FAILURE;
		}
	}
	else {
		CMrLock::m_pLock = (pthread_rwlock_t*)pLock;
	}

	return MR_SUCCESS;
}

int CMrRwLock::MrLockCreate(bool bShared)
{
	pthread_rwlockattr_t stAttr;

	if (NULL != CMrLock::m_pLock) {
		if (MR_SUCCESS !=  pthread_rwlockattr_init(&stAttr)) {
			MR_ERROR("%s\n", strerror(errno));
			return MR_FAILURE;
		}

		if (true == bShared) {
			if (MR_SUCCESS != pthread_rwlockattr_setpshared(&stAttr, PTHREAD_PROCESS_SHARED)) {
				MR_ERROR("%s\n", strerror(errno));
				return MR_FAILURE;
			}
		}

		if (MR_SUCCESS != pthread_rwlock_init((pthread_rwlock_t*)CMrLock::m_pLock, &stAttr)) {
			MR_ERROR("%s\n", strerror(errno));
			return MR_FAILURE;
		}

		if (MR_SUCCESS != pthread_rwlockattr_destroy(&stAttr)) {
			MR_ERROR("%s\n", strerror(errno));
			return MR_FAILURE;
		}
	}
	else {
		return MR_FAILURE;
	}

	return MR_SUCCESS;
}

int CMrRwLock::MrLockDelete()
{
	if (NULL != CMrLock::m_pLock) {
		if (MR_SUCCESS != pthread_rwlock_destroy((pthread_rwlock_t*)CMrLock::m_pLock)) {
			MR_ERROR("%s\n", strerror(errno));
			return MR_FAILURE;
		}
	}

	return MR_SUCCESS;
}

int CMrRwLock::MrLockUnlock()
{
	int iReturn = MR_FAILURE;

	if (NULL != CMrLock::m_pLock) {
		iReturn =  pthread_rwlock_unlock((pthread_rwlock_t*)CMrLock::m_pLock);
	}

	return iReturn;
}

int CMrRwLock::MrLockLock(UCHAR ucType)
{
	int iReturn = MR_FAILURE;

	switch (ucType) {
		case MR_RW_RDLK:
			iReturn = MrRwLockReadLock();
			break;
		case MR_RW_WRLK:
			iReturn = MrRwLockWriteLock();
			break;
		default:
			break;
	}

	return iReturn;
}

int CMrRwLock::MrLockTryLock(UCHAR ucType)
{
	int iReturn = MR_FAILURE;

	switch (ucType) {
		case MR_RW_RDLK:
			iReturn = MrRwLockTryReadLock();
			break;
		case MR_RW_WRLK:
			iReturn = MrRwLockTryWriteLock();
			break;
		default:
			break;
	}

	return iReturn;
}

int CMrRwLock::MrRwLockReadLock()
{
	int iReturn = MR_FAILURE;

	if (NULL != CMrLock::m_pLock) {
		iReturn = pthread_rwlock_rdlock((pthread_rwlock_t*)CMrLock::m_pLock);
	}

	return iReturn;
}

int CMrRwLock::MrRwLockWriteLock()
{
	int iReturn = MR_FAILURE;

	if (NULL != CMrLock::m_pLock) {
		iReturn =  pthread_rwlock_wrlock((pthread_rwlock_t*)CMrLock::m_pLock);
	}

	return iReturn;
}

int CMrRwLock::MrRwLockTryReadLock()
{
	int iReturn = MR_FAILURE;

	if (NULL != CMrLock::m_pLock) {
		iReturn = pthread_rwlock_tryrdlock((pthread_rwlock_t*)CMrLock::m_pLock);
	}

	return iReturn;
}

int CMrRwLock::MrRwLockTryWriteLock()
{
	int iReturn = MR_FAILURE;

	if (NULL != CMrLock::m_pLock) {
		iReturn =  pthread_rwlock_trywrlock((pthread_rwlock_t*)CMrLock::m_pLock);
	}

	return iReturn;
}
